# 根据输入页查询批次信息
import urllib.parse
import urllib.request


# 发送请求获取批次数据
def get_page(page):
    url = 'https://kq.coupon.18qy.cn:8001/api/wxx/batchInfo'
    data = {
        'pageSize': 10,
        'pageNum': page
    }
    data = urllib.parse.urlencode(data).encode('utf-8')
    headers = {
        'token': '8c536c8116eb46feaab348d905a8fdad'
    }
    request = urllib.request.Request(url=url, data=data, headers=headers)
    response = urllib.request.urlopen(request)
    content = response.read().decode('utf-8')
    return content


# 数据保存到文件中
def save_file(content, page):
    with open('batch_' + str(page) + '.json', 'w', encoding='utf-8') as fp:
        fp.write(content)


# 入口函数
if __name__ == '__main__':
    start_page = int(input('请输入开始页:'))
    end_page = int(input('请输入结束页:'))
    for page in range(start_page, end_page + 1):
        print('开始获取第%s页数据' % page)
        # 发送请求
        content = get_page(page)
        # 保存数据
        save_file(content, page)
